<?php
/**
 * CEOfox
 *
 * @copyright  Copyright 2011-2012 CEOfox, Inc.
 * @license    http://ceofox.com/pages/license
 * @version    $Id: follow.class.php 97089 2013-05-24 09:40:17 $
 * @author     CEOfox, Inc.
 */
?>
<?php

defined('PHPFOX') or exit('NO DICE!');

class Follow_Component_Block_Follow extends Phpfox_Component
{
    public function process()
    {
        $aUser = $this->getParam('aUser');
        $bIsCurrentUser = false;
        $bIsLogin = (phpfox::getUserId() >0)? true: false;
        if(phpfox::getUserId() == $aUser['user_id'])
        {
            $bIsCurrentUser = true;
        }
        phpfox::getService('follow')->checkBlockUser($aUser['user_id']);
        $bCanFollow = phpfox::getService('follow')->canFollow($aUser['user_id']);
        if($bCanFollow == false)
        {
            $this->template()->assign(array(
                'sError' => phpfox::getPhrase('follow.you_can_not_follow_this_user')
            ));
        }
        if($aVals = $this->request()->getArray('val'))
        {
            if(isset($aVals['following_id_too']))
            {
                $bIsFollow = phpfox::getService('follow')->isFollow($aVals['following_id_too']);
                if($bIsFollow)
                {
                    $this->template()->assign(array(
                        'sError' => phpfox::getPhrase('follow.you_already_follow_this_user')
                    ));
                }
                else
                {
                    $iId = phpfox::getService('follow')->follow($aVals['following_id_too']);
                    if(!isset($iId))
                    {
                        $this->template()->assign(array(
                            'sError' => phpfox::getPhrase('follow.this_user_is_block_you_follow')
                        ));
                    }    
                }
                
            }elseif(isset($aVals['following_id']))
            {
                phpfox::getService('follow')->unFollow($aVals['following_id']);
            }
        }
        $aUser['user_link'] = $this->url()->makeUrl('').$aUser['user_name'];
        
        $iFollower = phpfox::GetService('follow')->countFollower($aUser['user_id']);
        $iFollowing = phpfox::GetService('follow')->countFollowing($aUser['user_id']);
        $bIsFollow = phpfox::getService('follow')->isFollow(Phpfox::getUserId(),$aUser['user_id'],'user');
     
        $this->template()->setHeader('jquery/ui.js', 'static_script');
        $this->template()->setHeader('cache', array(
                    'pager.css' => 'style_css',                        
                    'friend.js' => 'module_friend',
                    'follow.js' => 'module_follow',
                    'friend.css' => 'style_css',
                    'follow.css' => 'module_follow'
                )
            ); 
        $this->template()->assign(array(
                'sCoreUrl' =>phpfox::getParam('core.path'),
                'sHeader' => Phpfox::getPhrase('follow.follow_information'),
                'bIsFollow' => $bIsFollow,
                'aUser' => $aUser,
                'sType' => 'user',
                'iFollower' =>  $iFollower,
                'iFollowing' =>  $iFollowing,
                'bIsCurrentUser' => $bIsCurrentUser,
                'bIsLogin' => $bIsLogin
        ));
        return 'block';
    }
}
?>

